package com.cnswhy.cloud.server.comsumer.consumer;

import com.cnswhy.cloud.common.core.exception.BizException;
import com.cnswhy.cloud.common.core.mybatis.conditions.Wraps;
import com.cnswhy.cloud.common.rabbitmq.service.MessageService;
import com.cnswhy.cloud.common.redis.service.IdGeneratorService;
import com.cnswhy.cloud.common.redis.service.RedisService;
import com.cnswhy.cloud.server.common.entity.constant.AmqpExchange;
import com.cnswhy.cloud.server.common.entity.enumeration.OrderReportType;
import com.cnswhy.cloud.server.common.entity.mq.CpsOrderQueryMQ;
import com.cnswhy.cloud.server.common.entity.mq.OrderReportMQ;
import com.cnswhy.cloud.server.comsumer.event.CpsOrderQueryEvent;
import com.cnswhy.cloud.server.core.channel.hcpay.dto.TransQueryDto;
import com.cnswhy.cloud.server.core.channel.hcpay.dto.TransQueryResponseDto;
import com.cnswhy.cloud.server.core.channel.hcpay.service.HcpayService;
import com.cnswhy.cloud.server.core.entity.CpsChannelInfo;
import com.cnswhy.cloud.server.core.entity.CpsTransOrder;
import com.cnswhy.cloud.server.core.oem.SystemService;
import com.cnswhy.cloud.server.core.service.CpsBankRateService;
import com.cnswhy.cloud.server.core.service.CpsTransOrderService;
import com.cnswhy.cloud.server.core.service.cps.CpsChannelInfoService;
import com.cnswhy.cloud.server.core.service.customer.CustomerBankService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class CpsOrderQueryConsumer implements CpsOrderQueryEvent {
    @Autowired
    RedisService redisService;
    @Autowired
    IdGeneratorService idGeneratorService;
    @Autowired
    CustomerBankService customerBankService;
    @Autowired
    SystemService systemService;
    @Autowired
    CpsChannelInfoService cpsChannelInfoService;
    @Autowired
    CpsBankRateService cpsBankRateService;
    @Autowired
    CpsTransOrderService cpsTransOrderService;
    @Autowired
    HcpayService hcpayService;
    @Autowired
    MessageService messageService;

    @Override
    public void query(CpsOrderQueryMQ cpsOrderQueryMQ) throws BizException {
        CpsTransOrder cpsTransOrder = cpsTransOrderService.getById(cpsOrderQueryMQ.getCpsId());
        if(cpsTransOrder == null){
            log.error("无效CPS_TRANS_ORDER_ID:{}", cpsOrderQueryMQ.getCpsId());
            return;
        }
        CpsChannelInfo cpsChannelInfo = cpsChannelInfoService.getOne(Wraps.<CpsChannelInfo>lbQ().eq(CpsChannelInfo::getChannelId,cpsTransOrder.getChannelId()));
        TransQueryResponseDto transQueryResponseDto = null;
        try {
            transQueryResponseDto = hcpayService.transQuery(TransQueryDto.builder().orderId(cpsTransOrder.getOrderId()).build(), cpsChannelInfo.getChannelMerchId(),cpsChannelInfo.getChannelMerchPrivateKey());
        }catch (BizException e){
            if(e.getMessage().equals("交易记录不存在") && cpsOrderQueryMQ.getQueryTimes().intValue() > 5) {
                cpsTransOrder.setStatus(3);
                cpsTransOrder.setRespCode("0001");
                cpsTransOrder.setRespDesc(e.getMessage());
                cpsTransOrderService.updateById(cpsTransOrder);
                return ;
            }

        }
        if(transQueryResponseDto == null){
            cpsOrderQueryMQ.setQueryTimes(cpsOrderQueryMQ.getQueryTimes() + 1);
            messageService.sendAndQueryTimes(AmqpExchange.CPS_ORDER_QUERY_MESSAGE, cpsOrderQueryMQ,cpsOrderQueryMQ.getQueryTimes());
            return;
        }
        if("02".equals(transQueryResponseDto.getOrderStatus())){
            //交易成功
            cpsTransOrder.setStatus(2);
            cpsTransOrder.setRespCode(transQueryResponseDto.getRespCode());
            cpsTransOrder.setRespDesc(transQueryResponseDto.getRespDesc());
            cpsTransOrderService.updateById(cpsTransOrder);
            //分账
            messageService.sendQueueByObject(AmqpExchange.CPS_ORDER_SPLIT_MESSAGE,CpsOrderQueryMQ.builder().cpsId(cpsOrderQueryMQ.getCpsId()).build());
            messageService.sendQueueByObject(AmqpExchange.REPORT_MESSAGE, OrderReportMQ.builder().orderId(cpsTransOrder.getOrderId())
                    .transAmt(cpsTransOrder.getTransAmt())
                    .orderReportType(OrderReportType.CPS_ORDER)
                    .success(true).build());
        }else if("03".equals(transQueryResponseDto.getOrderStatus())){
            //交易失败
            cpsTransOrder.setStatus(3);
            cpsTransOrder.setRespCode(transQueryResponseDto.getRespCode());
            cpsTransOrder.setRespDesc(transQueryResponseDto.getRespDesc());
            cpsTransOrderService.updateById(cpsTransOrder);
        }else{
            cpsOrderQueryMQ.setQueryTimes(cpsOrderQueryMQ.getQueryTimes() + 1);
            messageService.sendAndQueryTimes(AmqpExchange.CPS_ORDER_QUERY_MESSAGE, cpsOrderQueryMQ,cpsOrderQueryMQ.getQueryTimes());
            return;
        }


    }
}
